Option Strict Off
Option Explicit On
Imports System.Diagnostics.Process
Imports System.Threading
Imports System.Net.Mail

Public Class Connection
    Public WithEvents SBO_Application As SAPbouiCOM.Application
    Public ocompany As New SAPbobsCOM.Company
    Public SboGuiApi As New SAPbouiCOM.SboGuiApi
    Public sConnectionString As String
    Dim oF_SI_JobOrder As F_SI_JobOrder
    Dim oF_SalesOrder As F_SalesOrder
    Dim oF_AWB As F_AWB
    Dim oPrepareAWB As PrepareAWB

    Public Sub conn2()
        Dim sconn As String
        Dim ret As Integer
        Dim scook As String
        Dim str As String
        Try
            sconn = Environment.GetCommandLineArgs.GetValue(1)
            SboGuiApi.Connect(sconn)
            SBO_Application = SboGuiApi.GetApplication
            SboGuiApi = Nothing
            scook = ocompany.GetContextCookie
            str = SBO_Application.Company.GetConnectionContext(scook)
            ret = ocompany.SetSboLoginContext(str)
            ocompany.Connect()
            ocompany.GetLastError(ret, str)
            If ret <> 0 Then
                SBO_Application.MessageBox("SAP Connection Failed :" & str)
            End If
        Catch ex As Exception
            SBO_Application.MessageBox(ex.Message)
        End Try
    End Sub
#Region "Connection"
    Public Sub New()
        MyBase.new()
        Try
            conn2()
            LoadFromXML("MyMenus.xml", SBO_Application)
            oF_SI_JobOrder = New F_SI_JobOrder(ocompany, SBO_Application)
            oF_SalesOrder = New F_SalesOrder(ocompany, SBO_Application)
            oF_AWB = New F_AWB(ocompany, SBO_Application)
            'oPrepareAWB = New PrepareAWB(ocompany, SBO_Application)
            SBO_Application.MessageBox("Welcome To FM...")
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub conn1()
        Dim sconn As String
        Dim ret As Integer
        Dim scook As String
        Dim str As String
        Try
            sconn = Environment.GetCommandLineArgs.GetValue(1)
            SboGuiApi.Connect(sconn)
            SBO_Application = SboGuiApi.GetApplication
            SboGuiApi = Nothing
            scook = ocompany.GetContextCookie
            str = SBO_Application.Company.GetConnectionContext(scook)
            ret = ocompany.SetSboLoginContext(str)
            ocompany.Connect()
            ocompany.GetLastError(ret, str)
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub

#End Region
    Private Sub SBO_Application_AppEvent(ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles SBO_Application.AppEvent
        If EventType = SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged Then
            SBO_Application.StatusBar.SetText("Shuting Down addon", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Windows.Forms.Application.Exit()
        End If

        If EventType = SAPbouiCOM.BoAppEventTypes.aet_ShutDown Then
            SBO_Application.StatusBar.SetText("Shuting Down addon", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Windows.Forms.Application.Exit()
        End If

        If EventType = SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition Then
            SBO_Application.StatusBar.SetText("Shuting Down addon", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Windows.Forms.Application.Exit()
        End If
    End Sub

    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent

    End Sub
  
    Private Sub SBO_Application_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.MenuEvent
        Try
            If pVal.BeforeAction = True Then
                If pVal.MenuUID = "FMMySubMenu01" Then
                    LoadFromXML("SEA_IMPORT_JOBORDER.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("SEAI_JOB")
                    oF_SI_JobOrder.SI_Job_Bind(oForm)
                End If
                If pVal.MenuUID = "FMMySubMenu02" Then
                    SBO_Application.ActivateMenuItem("2050")
                End If
                If pVal.MenuUID = "HAWB" Or pVal.MenuUID = "MAWB" Then
                    Dim formCreationParams As SAPbouiCOM.FormCreationParams
                    Dim oXmlDoc As New Xml.XmlDocument
                    Dim sPath As String
                    Dim sFileName As String = ""
                    Try

                        Select Case pVal.MenuUID
                            Case "HAWB"
                                sFileName = "HAWB.srf"
                            Case "MAWB"
                                sFileName = "MAWB.srf"
                        End Select

                        sPath = IO.Directory.GetParent(Application.StartupPath).ToString
                        oXmlDoc.Load(sPath & "\GK_FM\" & sFileName)
                        formCreationParams = SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)
                        formCreationParams.XmlData = oXmlDoc.InnerXml
                        oForm = SBO_Application.Forms.AddEx(formCreationParams)
                        oF_AWB.AWB_Bind(oForm)
                    Catch ex As Exception
                        SBO_Application.MessageBox(ex.Message)
                    End Try
                End If
            End If
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub


End Class
